let house_id = 0
Page({
  data: {
    dialogVisible: false
  },

  async onShow() {
    this.getHouseList()
  },

  async getHouseList() {
    // 请求数据接口
    const { code, data: houseList } = await wx.http.get('/room')
    // 检测接口返回的结果
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ houseList })
  },

  swipeClose(ev) {
    const { position, instance } = ev.detail

    house_id = ev.mark.id

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  
  async dialogClose(event) {
    console.log('弹窗关闭', event.detail);
    if (event.detail === 'confirm') {
      // 确认删除
      // 请求数据接口
      const { code } = await wx.http.delete('/room/' + house_id)
      // 检测接口调用结果
      if (code !== 10000) return wx.utils.toast('删除房屋失败!')
      // 记得清理当前页面的数据
      const newList = this.data.houseList.filter(item=>item.id !== house_id)
      this.setData({
        houseList: newList
      })
    }
  },

  goDetail(event) {
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${event.mark.id}`
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  }
})
